﻿using System;
using System.Collections.Generic;
using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;

namespace TestDxfDocument
{
    
    class toolHolder
    {
        public static void Turning(Dictionary<string, Object> data)    //平装刀杆
        {
            DxfDocument dxf = new DxfDocument();

            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            double pi = Math.PI;

            double lf = Convert.ToDouble(data["lf"]);
            double wf = Convert.ToDouble(data["wf"]);
            double hf = Convert.ToDouble(data["hf"]);
            double b = Convert.ToDouble(data["b"]);
            double h = Convert.ToDouble(data["h"]);
            double kapr = Convert.ToDouble(data["kapr"]) * pi / 180;
            double lh = Convert.ToDouble(data["lh"]);
            double ic = Convert.ToDouble(data["ic"]);
            double re = Convert.ToDouble(data["re"]);
            double s = Convert.ToDouble(data["s"]);
            double d1 = Convert.ToDouble(data["d1"]);
            double epsr = Convert.ToDouble(data["epsr"]) * pi / 180;
            double an = Convert.ToDouble(data["an"]) * pi / 180;
            double a = Convert.ToDouble(data["a"]);
            double e = Convert.ToDouble(data["e"]);
            string path = Convert.ToString(data["path"]);

            double le = ic / Math.Sin(epsr);
            double ang1 = kapr - pi / 2;
            double ang2 = pi - kapr - epsr;
            double ang3 = pi - epsr;
            double ang4 = ang3 / 2;

            Vector2 p0 = new Vector2(0, 0);
            Vector2 p1 = new Vector2(p0.X - lf, p0.Y - wf);
            Vector2 p2 = fun.Polar(p1, ang1, le);
            Vector2 p3 = fun.Polar(p2, ang1 + epsr, le);
            Vector2 p4 = fun.Polar(p1, ang1 + epsr, le);
            Vector2 p5 = fun.Inters(p1, p3, p2, p4);
            Vector2 p1p = fun.Polar(p1, epsr / 2 + ang1, re / Math.Sin(epsr / 2));
            Vector2 p2p = fun.Polar(p2, pi - ang4 + ang1, re / Math.Sin(ang4));
            Vector2 p3p = fun.Polar(p3, pi + ang1 + epsr / 2, re / Math.Sin(epsr / 2));
            Vector2 p4p = fun.Polar(p4, ang1 - ang4, re / Math.Sin(ang4));
            Vector2 p6 = fun.Polar(p1, ang1 + epsr, re / Math.Tan(epsr / 2));
            Vector2 p7 = fun.Polar(p1, ang1, re / Math.Tan(epsr / 2));
            Vector2 p8 = fun.Polar(p2, pi + ang1, re / Math.Tan(ang4));
            Vector2 p9 = fun.Polar(p2, ang1 + epsr, re / Math.Tan(ang4));
            Vector2 p10 = fun.Polar(p3, 1.5 * pi - ang2, re / Math.Tan(epsr / 2));
            Vector2 p11 = fun.Polar(p3, pi + ang1, re / Math.Tan(epsr / 2));
            Vector2 p12 = fun.Polar(p4, ang1, re / Math.Tan(ang4));
            Vector2 p13 = fun.Polar(p4, 1.5 * pi - ang2, re / Math.Tan(ang4));
            Vector2 p20 = new Vector2(p0.X, p0.Y - b);
            Vector2 p21 = fun.Polar(p9, ang1 + epsr, 0.5);
            Vector2 p22 = fun.Polar(p12, ang1, 0.5);
            Vector2 p23 = fun.Inters(new Vector2(p1.X - lh, p1.Y - 10), new Vector2(p1.X - lh, p0.Y), p21, fun.Polar(p21, ang1, 100));
            Vector2 p24 = new Vector2(p23.X - 1, p20.Y);
            Vector2 p25 = fun.Polar(p22, ang1 + epsr, b / 3);
            Vector2 p26 = fun.Inters(p25, fun.Polar(p25, pi / 4, 100), p0, fun.Polar(p0, pi, 1000));
            Vector2 p30 = new Vector2(p1.X, p1.Y - b * 3);
            Vector2 p31 = fun.Polar(p30, 1.5 * pi + an, s / Math.Cos(an));
            Vector2 p32 = new Vector2(p31.X, p30.Y - hf);
            Vector2 p33 = new Vector2(p23.X, p32.Y);
            Vector2 p34 = new Vector2(p24.X, p32.Y);
            Vector2 p35 = new Vector2(p0.X, p32.Y);
            Vector2 p36 = fun.Polar(p35, pi / 2, h);
            Vector2 p37 = new Vector2(p24.X, p36.Y);
            Vector2 p38 = new Vector2(p23.X, p30.Y);
            Vector2 p39 = new Vector2(p21.X, p31.Y);
            Vector2 p40 = new Vector2(p2.X, p30.Y);
            //刀片轮廓
            Line insertline1 = new Line(p7, p8);
            Line insertline2 = new Line(p9, p10);
            Line insertline3 = new Line(p11, p12);
            Line insertline4 = new Line(p13, p6);
            Arc insertarc1 = fun.DrawArc(p6, p1p, p7);
            Arc insertarc2 = fun.DrawArc(p8, p2p, p9);
            Arc insertarc3 = fun.DrawArc(p10, p3p, p11);
            Arc insertarc4 = fun.DrawArc(p12, p4p, p13);
            Circle insertcircle1 = new Circle(p5, d1 / 2);
            insertline1.Layer = contour;
            insertline2.Layer = contour;
            insertline3.Layer = contour;
            insertline4.Layer = contour;
            insertarc1.Layer = contour;
            insertarc2.Layer = contour;
            insertarc3.Layer = contour;
            insertarc4.Layer = contour;
            insertcircle1.Layer = contour;
            dxf.AddEntity(insertline1);
            dxf.AddEntity(insertline2);
            dxf.AddEntity(insertline3);
            dxf.AddEntity(insertline4);
            dxf.AddEntity(insertarc1);
            dxf.AddEntity(insertarc2);
            dxf.AddEntity(insertarc3);
            dxf.AddEntity(insertarc4);
            dxf.AddEntity(insertcircle1);
            //刀杆轮廓
            Line turnline1 = new Line(p21, p23);
            Line turnline2 = new Line(p23, p24);
            Line turnline3 = new Line(p24, p20);
            Line turnline4 = new Line(p20, p0);
            Line turnline5 = new Line(p0, p26);
            Line turnline6 = new Line(p26, p25);
            Line turnline7 = new Line(p25, p22);
            Line turnline8 = new Line(p30, p36);
            Line turnline9 = new Line(p36, p35);
            Line turnline10 = new Line(p35, p32);
            Line turnline11 = new Line(p32, p31);
            Line turnline12 = new Line(p31, p30);
            Line turnline13 = new Line(p31, p39);
            Line turnline14 = new Line(p39, p40);
            Arc turnarc1 = fun.DrawArc(fun.Polar(p10, 1.5 * pi - ang2, 0.1), p3p, fun.Polar(p11, pi + ang1, 0.1));
            turnline1.Layer = contour;
            turnline2.Layer = contour;
            turnline3.Layer = contour;
            turnline4.Layer = contour;
            turnline5.Layer = contour;
            turnline6.Layer = contour;
            turnline7.Layer = contour;
            turnline8.Layer = contour;
            turnline9.Layer = contour;
            turnline10.Layer = contour;
            turnline11.Layer = contour;
            turnline12.Layer = contour;
            turnline13.Layer = contour;
            turnline14.Layer = contour;
            turnarc1.Layer = contour;
            dxf.AddEntity(turnline1);
            dxf.AddEntity(turnline2);
            dxf.AddEntity(turnline3);
            dxf.AddEntity(turnline4);
            dxf.AddEntity(turnline5);
            dxf.AddEntity(turnline6);
            dxf.AddEntity(turnline7);
            dxf.AddEntity(turnline8);
            dxf.AddEntity(turnline9);
            dxf.AddEntity(turnline10);
            dxf.AddEntity(turnline11);
            dxf.AddEntity(turnline12);
            dxf.AddEntity(turnline13);
            dxf.AddEntity(turnline14);
            dxf.AddEntity(turnarc1);

            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.2;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            dimensionStyle.TextHeight = 1.0 / 2;

            double offset = 10;
            LinearDimension dim1 = new LinearDimension(new Line(p1, p20), 3 * offset, 180, dimensionStyle);
            dim1.UserText = String.Format("{0}", lf);
            dxf.AddEntity(dim1);

            LinearDimension dim2 = new LinearDimension(new Line(p1, p23), 2 * offset, 180, dimensionStyle);
            dim2.UserText = String.Format("{0}", lh);
            dxf.AddEntity(dim2);

            LinearDimension dim3 = new LinearDimension(new Line(p0, p20), offset, 270, dimensionStyle);
            dim3.UserText = String.Format("{0}", b);
            dxf.AddEntity(dim3);

            LinearDimension dim4 = new LinearDimension(new Line(p35, p36), offset, 270, dimensionStyle);
            dim4.UserText = String.Format("{0}", h);
            dxf.AddEntity(dim4);

            LinearDimension dim5 = new LinearDimension(new Line(p26, p1), offset, 90, dimensionStyle);
            dim4.UserText = String.Format("{0}", wf);
            dxf.AddEntity(dim5);

            LinearDimension dim6 = new LinearDimension(new Line(p30, p32), offset, 90, dimensionStyle);
            dim5.UserText = String.Format("{0}", hf);
            dxf.AddEntity(dim6);


            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }

        public static void angTurning(Dictionary<string, Object> data)    //非平装刀杆
        {
            DxfDocument dxf = new DxfDocument();

            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            double pi = Math.PI;

            double lf = 170;//Convert.ToDouble(data["lf"]);
            double wf = 40;// Convert.ToDouble(data["wf"]);
            int hf = 32;// Convert.ToDouble(data["hf"]);
            int b = 32;// Convert.ToDouble(data["b"]);
            int h = 32;// Convert.ToDouble(data["h"]);
            double kapr = 95.0 * pi / 180;// Convert.ToDouble(data["kapr"]) * pi / 180;
            double lh = 32;// Convert.ToDouble(data["lh"]);
            double ic = 15.875;// Convert.ToDouble(data["ic"]);
            double re = 0.8;// Convert.ToDouble(data["re"]);
            double s = 6.35;// Convert.ToDouble(data["s"]);
            double d1 = 6.35;// Convert.ToDouble(data["d1"]);
            double epsr = 80 * pi / 180;// Convert.ToDouble(data["epsr"]) * pi / 180;
            double an = 0;// Convert.ToDouble(data["an"]) * pi / 180;
            double gamp = 6 * pi / 180;// Convert.ToDouble(data["gamp"]) * pi / 180;
            double gamf = 6 * pi / 180;// Convert.ToDouble(data["gamf"]) * pi / 180;
            string path = "E:\\dxfp\\angturn.dxf";// Convert.ToString(data["path"]);

            double le = ic / Math.Sin(epsr);
            double ang1 = kapr - pi / 2;
            double ang2 = pi - kapr - epsr;


            Vector2 p0 = new Vector2(0, 0);
            Vector2 p1 = new Vector2(p0.X - lf, p0.Y - wf);
            Vector2 p2 = fun.Polar(p1, ang1, le * Math.Cos(gamp));
            double vbb = le * Math.Sin(gamf);
            double vdd = le * Math.Sin(gamp);
            double vbd = 2 * le * Math.Sin(epsr / 2);
            double angle1 = Math.Acos((le * le * (Math.Cos(gamf) * Math.Cos(gamf) + Math.Cos(gamp) * Math.Cos(gamp)) - (vbd * vbd - (vbb - vdd) * (vbb - vdd))) / (2 * le * le * Math.Cos(gamf) * Math.Cos(gamp)));
            Vector2 p3 = fun.Polar(p2, Math.Abs(angle1), le * Math.Cos(gamp));
            Vector2 p4 = fun.Polar(p1, angle1, le * Math.Cos(gamp));
            Vector2 p5 = fun.Inters(p1, p3, p2, p4);
            Vector2 p10 = new Vector2(p1.X, p1.Y - b * 3.5);
            Vector2 p11 = fun.Polar(p10, gamp, le * Math.Cos(gamp) * Math.Cos(ang1));
            Vector2 p13 = fun.Polar(p10, 1.5 * pi + gamp, s * Math.Cos(gamp));
            Vector2 p12 = fun.Polar(p11, 1.5 * pi + gamp, s * Math.Cos(gamp));
            Vector2 p14 = fun.Inters(p4, fun.Polar(p4, 1.5 * pi, 100), p10, fun.Polar(p10, gamp + gamf, 100));
            Vector2 p15 = fun.Polar(p14, gamp, le * Math.Cos(gamp) * Math.Cos(ang1));
            Vector2 p17 = fun.Polar(p12, 1.5 * pi + gamp, s * Math.Cos(gamp));
            Vector2 p16 = fun.Polar(p13, 1.5 * pi + gamp, s * Math.Cos(gamp));
            Vector2 p18 = new Vector2(p16.X, p10.Y - hf);
            Vector2 p19 = new Vector2(p10.X + lf, p18.Y);
            Vector2 p20 = fun.Polar(p19, 0.5 * pi, h);
            Vector2 p21 = new Vector2(p10.X + lh, p20.Y);
            Vector2 p22 = fun.Polar(p21, 1.5 * pi, h);
            Vector2 p31 = fun.Polar(p0, 1.5 * pi, b);
            Vector2 p32 = fun.Polar(p31, pi, lf - lh);
            Vector2 p33 = fun.Polar(p0, pi, lf - lh);
            Vector2 p23 = fun.Inters(p11, fun.Polar(p11, gamp, 100), p22, fun.Polar(p22, 0.53 * pi, 100));
            Vector2 p24 = fun.Inters(p21, fun.Polar(p21, 0.6 * pi, 100), p23, fun.Polar(p23, 0.35 * pi, 100));
            Vector2 p25 = fun.Polar(p14, gamp, 0.3);
            Vector2 p26 = fun.Inters(p25, fun.Polar(p25, gamp + gamf, 100), p24, fun.Polar(p24, pi + gamp, 100));
            Vector2 p34 = new Vector2(p24.X, p0.Y);
            Vector2 p35 = fun.Polar(p34, pi, lh / 2);
            Vector2 p37 = fun.Polar(p4, ang1, 0.4);
            Vector2 p36 = fun.Inters(p37, fun.Polar(p37, angle1, 100), p35, fun.Polar(p35, 1.25 * pi, 100));
            Vector2 p38 = fun.Polar(p2, angle1, 0.4);
            Vector2 p39 = fun.Inters(p38, fun.Polar(p38, gamp, 100), p23, fun.Polar(p23, pi / 2, 100));
            Vector2 p50 = fun.Polar(p1, pi, wf * 3);
            Vector2 p60 = new Vector2(p50.X - hf, p50.Y + wf);
            Vector2 p61 = new Vector2(p60.X, p50.Y + (p10.Y - p18.Y));

            Vector2[] line1 = { p1, p2, p3, p4, p1 };
            fun.drawPline(dxf, line1, 0.15, contour);
            Vector2[] line2 = { p10, p11, p12, p13, p10 };
            fun.drawPline(dxf, line2, 0.15, contour);
            Vector2[] line3 = { p11, p15, p14, p10 };
            fun.drawPline(dxf, line3, 0.15, contour);
            Vector2[] line4 = { p12, p13, p16, p17, p12 };
            fun.drawPline(dxf, line4, 0.15, contour);
            Vector2[] line5 = { p16, p18, p19, p20, p21, p22 };
            fun.drawPline(dxf, line5, 0.15, contour);
            Vector2[] line6 = { p33, p0, p31, p32, p33 };
            fun.drawPline(dxf, line6, 0.15, contour);
            Vector2[] line7 = { p11, p23, p22 };
            fun.drawPline(dxf, line7, 0.15, contour);
            Vector2[] line8 = { p23, p24, p21 };
            fun.drawPline(dxf, line8, 0.15, contour);
            Vector2[] line9 = { p25, p26, p24 };
            fun.drawPline(dxf, line9, 0.15, contour);
            Vector2[] line10 = { p33, p34, p35, p36, p37 };
            fun.drawPline(dxf, line10, 0.15, contour);
            Vector2[] line11 = { p38, p39, p32 };
            fun.drawPline(dxf, line11, 0.15, contour);
            Vector2[] line12 = { p34, p39 };
            fun.drawPline(dxf, line12, 0.15, contour);

            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.2;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            dimensionStyle.TextHeight = 2.0;
            double offset = 10;

            LinearDimension dim1 = new LinearDimension(new Line(p1, p31), offset * 2, 180, dimensionStyle);
            dim1.UserText = String.Format("{0}", lf);
            dxf.AddEntity(dim1);

            LinearDimension dim2 = new LinearDimension(new Line(p1, p32), offset * 1.3, 180, dimensionStyle);
            dim2.UserText = String.Format("{0}", lh);
            dxf.AddEntity(dim2);

            LinearDimension dim3 = new LinearDimension(new Line(p0, p31), offset, 270, dimensionStyle);
            dim3.UserText = String.Format("{0}", b);
            dxf.AddEntity(dim3);

            LinearDimension dim4 = new LinearDimension(new Line(p19, p20), offset, 270, dimensionStyle);
            dim3.UserText = String.Format("{0}", h);
            dxf.AddEntity(dim4);

            LinearDimension dim5 = new LinearDimension(new Line(p1, p35), offset, 90, dimensionStyle);
            dim4.UserText = String.Format("{0}", Math.Round(wf,0));
            dxf.AddEntity(dim5);

            LinearDimension dim6 = new LinearDimension(new Line(p10, p18), offset, 90, dimensionStyle);
            dim5.UserText = String.Format("{0}", hf);
            dxf.AddEntity(dim6);



            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }

        public static void Example(Dictionary<string, Object> data)    //平装刀杆
        {
            DxfDocument dxf = new DxfDocument();

            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            double pi = Math.PI;

            double lf = Convert.ToDouble(data["lf"]);
            double wf = Convert.ToDouble(data["wf"]);
            double b = Convert.ToDouble(data["b"]);
            double h = Convert.ToDouble(data["h"]);
            double kapr = Convert.ToDouble(data["kapr"]) * pi / 180;
            double lh = Convert.ToDouble(data["lh"]);
            double ic = Convert.ToDouble(data["ic"]);
            double re = Convert.ToDouble(data["re"]);
            double d1 = Convert.ToDouble(data["d1"]);
            double epsr = Convert.ToDouble(data["epsr"]) * pi / 180;
            double an = Convert.ToDouble(data["an"]) * pi / 180;
            string path = Convert.ToString(data["path"]);

            double le = ic / Math.Sin(epsr);
            double ang1 = kapr - pi / 2;
            double ang2 = pi - kapr - epsr;
            double ang3 = pi - epsr;
            double ang4 = ang3 / 2;

            Vector2 p0 = new Vector2(0, 0);
            Vector2 p1 = new Vector2(p0.X - lf, p0.Y - wf);
            Vector2 p2 = fun.Polar(p1, ang1, le);
            Vector2 p3 = fun.Polar(p2, ang1 + epsr, le);
            Vector2 p4 = fun.Polar(p1, ang1 + epsr, le);
            Vector2 p5 = fun.Inters(p1, p3, p2, p4);
            Vector2 p1p = fun.Polar(p1, epsr / 2 + ang1, re / Math.Sin(epsr / 2));
            Vector2 p2p = fun.Polar(p2, pi - ang4 + ang1, re / Math.Sin(ang4));
            Vector2 p3p = fun.Polar(p3, pi + ang1 + epsr / 2, re / Math.Sin(epsr / 2));
            Vector2 p4p = fun.Polar(p4, ang1 - ang4, re / Math.Sin(ang4));
            Vector2 p6 = fun.Polar(p1, ang1 + epsr, re / Math.Tan(epsr / 2));
            Vector2 p7 = fun.Polar(p1, ang1, re / Math.Tan(epsr / 2));
            Vector2 p8 = fun.Polar(p2, pi + ang1, re / Math.Tan(ang4));
            Vector2 p9 = fun.Polar(p2, ang1 + epsr, re / Math.Tan(ang4));
            Vector2 p10 = fun.Polar(p3, 1.5 * pi - ang2, re / Math.Tan(epsr / 2));
            Vector2 p11 = fun.Polar(p3, pi + ang1, re / Math.Tan(epsr / 2));
            Vector2 p12 = fun.Polar(p4, ang1, re / Math.Tan(ang4));
            Vector2 p13 = fun.Polar(p4, 1.5 * pi - ang2, re / Math.Tan(ang4));
            Vector2 p20 = new Vector2(p0.X, p0.Y - b);
            Vector2 p21 = fun.Polar(p9, ang1 + epsr, 0.5);
            Vector2 p22 = fun.Polar(p12, ang1, 0.5);
            Vector2 p23 = fun.Inters(new Vector2(p1.X + lh - 1, p1.Y - 10), new Vector2(p1.X + lh - 1, p0.Y), p21, fun.Polar(p21, ang1, 100));
            Vector2 p24 = new Vector2(p23.X + 1, p20.Y);
            Vector2 p25 = fun.Polar(p22, ang1 + epsr, b / 3);
            Vector2 p26 = fun.Inters(p25, fun.Polar(p25, pi / 4, 100), p0, fun.Polar(p0, pi, 1000));
            Vector2 p41 = new Vector2(p1.X, p1.Y - 10);
            Vector2 p42 = new Vector2(p1.X + 10 * Math.Tan(180 * Math.PI / 180 - kapr), p1.Y + 10 * Math.Tan(kapr - 90 * Math.PI / 180));
            Vector2 v1 = new Vector2(-h, 0);
            Vector2 v2 = new Vector2(-2 * h, 0);
            Vector2 v3 = new Vector2(v2.X, -b);
            Vector2 v4 = new Vector2(v1.X, -b);
            //刀片轮廓
            Line insertline1 = new Line(p7, p8);
            Line insertline2 = new Line(p9, p10);
            Line insertline3 = new Line(p11, p12);
            Line insertline4 = new Line(p13, p6);
            Arc insertarc1 = fun.DrawArc(p6, p1p, p7);
            Arc insertarc2 = fun.DrawArc(p8, p2p, p9);
            Arc insertarc3 = fun.DrawArc(p10, p3p, p11);
            Arc insertarc4 = fun.DrawArc(p12, p4p, p13);
            Circle insertcircle1 = new Circle(p5, d1 / 2);
            insertline1.Layer = contour;
            insertline2.Layer = contour;
            insertline3.Layer = contour;
            insertline4.Layer = contour;
            insertarc1.Layer = contour;
            insertarc2.Layer = contour;
            insertarc3.Layer = contour;
            insertarc4.Layer = contour;
            insertcircle1.Layer = contour;
            dxf.AddEntity(insertline1);
            dxf.AddEntity(insertline2);
            dxf.AddEntity(insertline3);
            dxf.AddEntity(insertline4);
            dxf.AddEntity(insertarc1);
            dxf.AddEntity(insertarc2);
            dxf.AddEntity(insertarc3);
            dxf.AddEntity(insertarc4);
            dxf.AddEntity(insertcircle1);
            //刀杆轮廓
            Line turnline1 = new Line(p21, p23);
            Line turnline2 = new Line(p23, p24);
            Line turnline3 = new Line(p24, p20);
            Line turnline4 = new Line(p20, p0);
            Line turnline5 = new Line(p0, p26);
            Line turnline6 = new Line(p26, p25);
            Line turnline7 = new Line(p25, p22);
            Arc turnarc1 = fun.DrawArc(fun.Polar(p10, 1.5 * pi - ang2, 0.1), p3p, fun.Polar(p11, pi + ang1, 0.1));
            Line Line1 = new Line(v1, v4);
            Line Line2 = new Line(v2, v3);
            turnline1.Layer = contour;
            turnline2.Layer = contour;
            turnline3.Layer = contour;
            turnline4.Layer = contour;
            turnline5.Layer = contour;
            turnline6.Layer = contour;
            turnline7.Layer = contour;
            turnarc1.Layer = contour;
            Line1.Layer = refin;
            Line2.Layer = refin;
            dxf.AddEntity(turnline1);
            dxf.AddEntity(turnline2);
            dxf.AddEntity(turnline3);
            dxf.AddEntity(turnline4);
            dxf.AddEntity(turnline5);
            dxf.AddEntity(turnline6);
            dxf.AddEntity(turnline7);
            dxf.AddEntity(turnarc1);
            dxf.AddEntity(Line1);
            dxf.AddEntity(Line2);

            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.2;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            dimensionStyle.TextHeight = 1.0 / 2;

            double offset = 10;
            LinearDimension dim1 = new LinearDimension(new Line(p1, p20), 3 * offset, 180, dimensionStyle);
            dim1.UserText = "LF = " + Math.Round(lf, 2);
            dxf.AddEntity(dim1);

            LinearDimension dim2 = new LinearDimension(new Line(p1, p24), 2 * offset, 180, dimensionStyle);
            dim2.UserText = "LH = " + Math.Round(lh, 2);
            dxf.AddEntity(dim2);

            LinearDimension dim3 = new LinearDimension(new Line(p0, p20), offset, 270, dimensionStyle);
            dim3.UserText = "B = " + Math.Round(b, 2);
            dxf.AddEntity(dim3);

            LinearDimension dim5 = new LinearDimension(new Line(p26, p1), offset, 90, dimensionStyle);
            dim5.UserText = "WF = " + Math.Round(wf, 2);
            dxf.AddEntity(dim5);

            Angular2LineDimension dim7 = new Angular2LineDimension(new Line(p1, p41), new Line(p1, p42), offset, dimensionStyle);
            dim7.UserText = String.Format("KAPR={0}°", kapr / (pi / 180));
            dxf.AddEntity(dim7);

            LinearDimension dim8 = new LinearDimension(new Line(v3, v4), 2 * offset, 180, dimensionStyle);
            dim8.UserText = "H = " + Math.Round(h, 2);
            dxf.AddEntity(dim8);

            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v1));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v2));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v3));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v4));
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsubofline(dxf, htype1, hnumst1, 45, 5, hline1, hatching);

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
        public static void TurningToolNo1StraightDesign(Dictionary<string, Object> data)    //1号平装车刀杆
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
            //参数
            double pi = Math.PI;
            double slf = Convert.ToDouble(data["lf"]);//总长度
            double scale = Convert.ToDouble(data["scale"]);
            if (scale == 0)
            {
                if (slf < 53.8)
                {
                    scale = 5.0;
                }
                else if (slf < 67.25)
                {
                    scale = 4.0;
                }
                else if (slf < 90)
                {
                    scale = 3.0;
                }
                else if (slf < 134.5)
                {
                    scale = 2.0;
                }
                else if (slf < 269)
                {
                    scale = 1.0;
                }
                else if (slf < 359)
                {
                    scale = 0.75;
                }
                else
                {
                    scale = 0.5;
                }
            }
            double lf = Convert.ToDouble(data["lf"]) * scale;
            double wf = Convert.ToDouble(data["wf"]) * scale;
            double b = Convert.ToDouble(data["b"]) * scale;
            double h = Convert.ToDouble(data["h"]) * scale;
            double kapr = Convert.ToDouble(data["kapr"]) * pi / 180;
            double lh = Convert.ToDouble(data["lh"]) * scale;
            double l = Convert.ToDouble(data["l"]) * scale;
            double epsr = Convert.ToDouble(data["epsr"]) * pi / 180;
            double isframe = Convert.ToDouble(data["isframe"]);
            string path = Convert.ToString(data["path"]);
            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(b, 0);
            Vector2 v3 = new Vector2(v2.X, -lf + l * Math.Sin(kapr));
            Vector2 v4 = new Vector2(wf, -lf);
            Vector2 v5 = new Vector2(0, -lf + wf * Math.Tan(pi - kapr - epsr));
            Vector2 v6 = new Vector2(0, v3.Y + b * Math.Tan(pi - kapr - epsr));
            Vector2 v8 = new Vector2(wf / 2, -lf + wf / 2 * Math.Tan(pi - kapr - epsr));
            Vector2 v7 = new Vector2(v8.X + v3.X - v4.X, v8.Y + v3.Y - v4.Y);
            Vector2 v9 = new Vector2(0, -lf + lh);
            Vector2 v10 = new Vector2(v2.X, v9.Y - (v6.Y - v3.Y));
            Vector2 v11 = new Vector2(0, -(-v9.Y - h) / 2);
            Vector2 v12 = new Vector2(v2.X, v11.Y);
            Vector2 v13 = new Vector2(v11.X, v11.Y - h);
            Vector2 v14 = new Vector2(v12.X, v13.Y);
            //线段
            Vector2[] Line1 = { v1, v2 };
            fun.drawBLine(dxf, Line1, 0.3, contour);
            Vector2[] Line2 = { v2, v3 };
            fun.drawBLine(dxf, Line2, 0.3, contour);
            Vector2[] Line3 = { v3, v4 };
            fun.drawBLine(dxf, Line3, 0.3, contour);
            Vector2[] Line4 = { v4, v5 };
            fun.drawBLine(dxf, Line4, 0.3, contour);
            Vector2[] Line5 = { v5, v1 };
            fun.drawBLine(dxf, Line5, 0.3, contour);
            Vector2[] Line6 = { v6, v3 };
            fun.drawBLine(dxf, Line6, 0.3, contour);
            Vector2[] Line7 = { v7, v8 };
            fun.drawBLine(dxf, Line7, 0.3, contour);
            if (lh != 0)
            {
                Vector2[] Line8 = { v9, v10 };
                fun.drawBLine(dxf, Line8, 0.3, contour);
            }
            Vector2[] Line9 = { v11, v12 };
            fun.drawPlinetype(dxf, Line9, 0.15, refin, Linetype.Dashed);
            Vector2[] Line10 = { v13, v14 };
            fun.drawPlinetype(dxf, Line10, 0.15, refin, Linetype.Dashed);
            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v11));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v12));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v14));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v13));
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsubofline(dxf, htype1, hnumst1, 45, 2, hline1, hatching);
            //文本样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.2;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            dimensionStyle.TextHeight = 1.0;
            double offset = 10;
            //标注
            LinearDimension dim1 = new LinearDimension(new Line(v4, v2), 3 * offset + (v2.X - v4.X) / 2, 270, dimensionStyle);
            dim1.UserText = "LF = " + Math.Round(lf / scale, 2);
            dxf.AddEntity(dim1);
            if (lh != 0)
            {
                LinearDimension dim2 = new LinearDimension(new Line(v4, v9), -offset - v4.X / 2, 270, dimensionStyle);
                dim2.UserText = "LH = " + Math.Round(lh / scale, 2);
                dxf.AddEntity(dim2);
            }

            LinearDimension dim3 = new LinearDimension(new Line(v1, v2), offset, 0, dimensionStyle);
            dim3.UserText = "B = " + Math.Round(b / scale, 2);
            dxf.AddEntity(dim3);

            LinearDimension dim4 = new LinearDimension(new Line(v4, v3), -offset / 2, kapr / (pi / 180), dimensionStyle);
            dim4.UserText = "L = " + Math.Round(l / scale, 2);
            dxf.AddEntity(dim4);

            LinearDimension dim5 = new LinearDimension(new Line(v5, v4), -offset - (v5.Y - v4.Y) / 2, 0, dimensionStyle);
            dim5.UserText = "WF = " + Math.Round(wf / scale, 2);
            dxf.AddEntity(dim5);

            Angular2LineDimension dim6 = new Angular2LineDimension(new Line(v4, v8), new Line(v4, v3), offset / 2, dimensionStyle);
            dim6.UserText = String.Format("EPSR={0}°", epsr / (pi / 180));
            dxf.AddEntity(dim6);

            Angular2LineDimension dim7 = new Angular2LineDimension(new Line(v4, v3), new Line(v4, new Vector2(v3.X, v4.Y)), offset * 2, dimensionStyle);
            dim7.UserText = String.Format("KAPR={0}°", kapr / (pi / 180));
            dxf.AddEntity(dim7);

            LinearDimension dim8 = new LinearDimension(new Line(v11, v13), -offset, 270, dimensionStyle);
            dim8.UserText = "H = " + Math.Round(h / scale, 2);
            dxf.AddEntity(dim8);
            //图框
            if (isframe == 1)
            {
                Dictionary<string, Vector2> position = Frame.frameA3Y(dxf, new Vector2(v2.X - b / 2 - 148.5, v1.Y + 210 - lf / 2));
                fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
            }
            //文件格式
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}